from rest_framework.viewsets import ModelViewSet
from aaa import models
from rest_framework import serializers
from rest_framework.response import Response
import json
from django.http import JsonResponse
from aaa.utils.pager import myPageNumberPagination

class goodsSerializer(serializers.ModelSerializer):
    goods_cat = serializers.SerializerMethodField()
    class Meta:
        model = models.Goods
        fields = ['goods_id','goods_cat','goods_state','goods_name','goods_number','is_promote','hot_number',
                  'goods_price','goods_weight','sale_num','read_num','ori_price','pics','add_time','upd_time','attrs','goods_introduce']
    # def get_goods_cat(self,row):
    #     print('get_goods_cat row',row.goods_cat)
    #     cat_id,second_pid = row.goods_cat.cat_id,row.goods_cat.cat_pid
    #     if second_pid:
    #         first_pid = models.Categories.objects.get(cat_id=second_pid).cat_pid
    #         cat_id_list = []
    #         cat_id_list.append(first_pid)
    #         cat_id_list.append(second_pid)
    #         cat_id_list.append(cat_id)
    #         return cat_id_list

    def get_goods_cat(self,row):
        print('get_goods_cat row',row.goods_cat)
        obj = models.Categories.objects.get(cat_id=int(row.goods_cat))
        cat_id = obj.cat_id
        second_pid = models.Categories.objects.get(cat_id=cat_id).cat_pid
        if second_pid:
            first_pid = models.Categories.objects.get(cat_id=second_pid).cat_pid
            cat_id_list = []
            cat_id_list.append(first_pid)
            cat_id_list.append(second_pid)
            cat_id_list.append(cat_id)
            return cat_id_list

class goodsInfoView(ModelViewSet):
    queryset = models.Goods.objects.all()
    serializer_class = goodsSerializer
    # pagination_class = PageNumberPagination
    authentication_classes = []

    def list(self, request, *args, **kwargs):
        pg = myPageNumberPagination()
        xx = pg.paginate_queryset(queryset=self.queryset,request=request,view=self)
        ser = goodsSerializer(instance=xx,many=True)
        ret = {}
        ret['data'] = json.dumps(ser.data)
        ret['total'] = self.queryset.count()
        return JsonResponse(ret)

    def create(self, request, *args, **kwargs):
        print('addGoods res',request.data)
        data = request.data
        try:
            obj = models.Goods.objects.create(goods_name=data['goods_name'],goods_price=data['goods_price'],
                                              ori_price=data['ori_price'],
                                              goods_weight=data['goods_weight'],goods_number=data['goods_number'],
                                              sale_num=data['sale_num'],read_num=data['read_num'],goods_cat=data['goods_cat'][2],
                                              pics=data['pics'],goods_introduce=data['goods_introduce'],attrs=data['attrs'])
            return JsonResponse({'meta': {'status': 201, 'msg': 'ok'}})
        except Exception as e:
            print('e',e)

    def update(self, request, *args, **kwargs):
        print('updateGoods res', request.data)
        data = request.data
        try:
            obj = models.Goods.objects.filter(goods_id=data['goods_id']).update(
                    goods_name=data['goods_name'],goods_price=data['goods_price'],ori_price=data['ori_price'],
                    goods_weight=data['goods_weight'],goods_number=data['goods_number'],sale_num=data['sale_num'],
                    read_num=data['read_num'],
                    goods_cat=data['goods_cat'],pics=data['pics'],goods_introduce=data['goods_introduce'],
                    attrs=data['attrs'])
            return JsonResponse({'meta': {'status': 201, 'msg': 'ok'}})
        except Exception as e:
            print('e',e)
